/**
 * 
 */
package br.edu.flf.teoriagrafos.visao.controle;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Stroke;
import java.awt.Toolkit;
import java.util.List;

import br.edu.flf.teoriagrafos.controle.geral.vos.grafo.Grafo;
import br.edu.flf.teoriagrafos.controle.geral.vos.grafo.Vertice;
import br.edu.flf.teoriagrafos.testes.ThreadPrincipal;
import br.edu.flf.teoriagrafos.visao.janela.Moldura;
import br.edu.flf.teoriagrafos.visao.painel.GrafoPainel;
import br.edu.flf.teoriagrafos.visao.vo.grafo.No;
import br.edu.flf.teoriagrafos.visao.vo.shape.Circulo;

/**
 * @author Rafael Santiago
 * Data: 05/05/2011 - Hora: 16:11:41
 */
public final class ControleVisao {
	private static ControleVisao controleVisao = new ControleVisao();

	public static ControleVisao getInstance() {

		if (controleVisao == null) {
			controleVisao = new ControleVisao();
		}

		return controleVisao;
	}

	private ControleVisao() {

	}

	private void desenharNoFinal(Circulo circulo, Graphics2D g2d, Color color) {

		g2d.setColor(color);
		g2d.fillOval(circulo.getCoordenadaX(), circulo.getCoordenadaY(),
				circulo.getIntLargura(), circulo.getIntAltura());

		Stroke stroke = new BasicStroke(2.0f);
		g2d.setStroke(stroke);

		g2d.setColor(Color.RED);
		g2d.drawOval(circulo.getCoordenadaX(), circulo.getCoordenadaY(),
				circulo.getIntLargura(), circulo.getIntAltura());
		g2d.drawOval(circulo.getCoordenadaX() + 5,
				circulo.getCoordenadaY() + 5, circulo.getIntLargura() - 10,
				circulo.getIntAltura() - 10);
	}

	public static void desenhaLabel(String label, Graphics2D g2d, Point ponto,
			Color color) {

		g2d.setFont(new Font("SansSerif", Font.PLAIN, 18));

		g2d.setColor(color);
		g2d.drawString(label, (int) ponto.getX() + 11, (int) ponto.getY() + 24);

	}

	public static void desenharNo(Circulo circulo, Graphics2D g2d, Color color) {
		Stroke stroke = new BasicStroke(3.0f);
		g2d.setStroke(stroke);

		g2d.setColor(color);
		g2d.fillOval(circulo.getCoordenadaX() + 20, circulo.getCoordenadaY() + 20,
				circulo.getIntLargura(), circulo.getIntAltura());
		g2d.setColor(Color.RED);
		g2d.drawOval(circulo.getCoordenadaX() + 20,
				circulo.getCoordenadaY() + 20, circulo.getIntLargura(),
				circulo.getIntAltura());
	}

	public void desenhaSeta(Point pontoOrigem, Point pontoDestino, Graphics g,
			Color color) {

		Graphics2D g2d = (Graphics2D) g;
		g2d.setColor(color);

		Stroke stroke = new BasicStroke(2.0f, BasicStroke.CAP_BUTT,
				BasicStroke.JOIN_MITER);
		g2d.setStroke(stroke);

		// g2d.drawLine((int) pontoOrigem.getX(), (int) pontoOrigem.getY(),
		// (int) pontoDestino.getX(), (int) pontoDestino.getY());

		g2d.drawLine((int) pontoOrigem.getX(), (int) pontoOrigem.getY(),
				(int) pontoDestino.getX(), (int) pontoDestino.getY());

		g2d.drawLine((int) pontoDestino.getX(), (int) pontoDestino.getY(),
				(int) pontoDestino.getX(), (int) pontoDestino.getY() - 10);
		g2d.drawLine((int) pontoDestino.getX(), (int) pontoDestino.getY(),
				(int) pontoDestino.getX() - 10, (int) pontoDestino.getY());

	}	

	synchronized public void configuraGrafoTela(Grafo grafoVO, GrafoPainel content) {
		
		List<No> estados = Moldura.estados;
		
		if(grafoVO == null){
			return;
		}
		
		// List<ArestaVO> arestas = grafoVO.getArestas();
		List<Vertice> vertices = grafoVO.getGrafVertices();	

		No no = null;

		Dimension tamanhoTela = Toolkit.getDefaultToolkit ().getScreenSize();				
		
		int x = 0;
		int y = 0;

		System.out.println(x);
		System.out.println(y);
		
		for (Vertice noVO : vertices) {
			no = new No(x, y);
			//no.setLabel(noVO.getVertId());
			
			
			estados.add(no);
			

			if (x == y) {
				y += 50 * 3;
			} else if (y > x) {
				int aux = x;
				x = y;
				y = aux;
			} else {
				y = x;
			}

			try {
				Thread.sleep(1500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}		
			
			ThreadPrincipal.moldura.getContentPane().repaint();
			//content.repaint();			
		}

	}

	/*
	 * public List<ArestaFormVO> mapearArestasNoPainel(GrafoVO grafoVO){ int
	 * qtd_nos = grafoVO.getNos().size(); int x, y = 0; List<Point> pontos;
	 * 
	 * if(qtd_nos == 4){
	 * 
	 * List<ArestaVO> arestas = grafoVO.getArestas();
	 * 
	 * for (ArestaVO arestaVO : arestas) {
	 * 
	 * }
	 * 
	 * }
	 * 
	 * 
	 * 
	 * return null; }
	 */
}
